﻿using System;
using WorkOrder.Model;

namespace WorkOrder.Business
{
    public class Business_Table_Process
    {
        DbHelper dbhelper = new DbHelper();

        #region 保存
        public bool Insert(Model_Table_Process model)
        {
            string sqlstr = " insert into Table_Process (process,timeout,time_alarm,remark) values('";
            sqlstr += model.Process + "'," + model.Timeout + "," + model.Time_alarm + ",'" + model.Remark + "')";
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 更新
        public bool Update(Model_Table_Process model)
        {
            string sql = "update Table_Process set process='" + model.Process
                + "',timeout=" + model.Timeout
                + ",time_alarm=" + model.Time_alarm + ",remark='" + model.Remark
                + "' where process_id=" + model.Process_id;
            if (dbhelper.ExecuteNoQuery(sql) > 0)
                return true;
            else
                return false;
        }
        #endregion

        #region 检查记录是否被引用
        /// <summary>
        /// 检查记录是否被引用
        /// </summary>
        /// <returns>true：已被引用，false：未被引用</returns>
        public bool hasUsed(string keyValue)
        {
            string sql = "select top 1 process_id from Table_Order_record where process_id=" + keyValue;
            return dbhelper.ExecuteScalar(sql) != null;
        }
        #endregion

        #region 得到单个数据集
        public Model_Table_Process GetData(string keyValue)
        {
            string sql = "select * from Table_Process where process_id=" + keyValue;
            using (System.Data.SqlClient.SqlDataReader rdr = dbhelper.Search(sql))
            {
                if (rdr != null && rdr.Read())
                {
                    Model_Table_Process tmp;
                    tmp = new Model_Table_Process(rdr["process_id"].ToString(), rdr["process"].ToString(), Convert.ToDecimal(rdr["timeout"]), Convert.ToDecimal(rdr["time_alarm"]),
                        rdr["remark"].ToString());
                    rdr.Close();
                    dbhelper.CloseDbConnection();
                    return tmp;
                }
                else
                    return null;
            }
        }
        #endregion

        #region 根据条件得到数据集
        public System.Data.DataSet GetDatas(string condition)
        {
            if (condition == "") condition = " 1=1";
            string sqlstr = "select * from Table_Process where" + condition + " order by process_id";
            return dbhelper.Query(sqlstr);
        }
        #endregion

        #region 删除记录
        public bool Delete(string fieldName, string fieldValue)
        {
            string sqlstr = "delete Table_Process where " + fieldName + "=" + fieldValue;
            if (dbhelper.ExecuteNoQuery(sqlstr) > 0)
                return true;
            else
                return false;
        }
        #endregion

    }
}
